﻿
$(document).ready(function () {
    
    var company = $("#ddlCompany").find("option:selected").text();

    if (company == "") {
        $("#ddlFK_ID").attr("disabled", "disabled"); //部门禁用
        $("#ddlFK_ID").empty(); //清空下拉框值
        $("input[id$='hfid']").val(""); //清空
    } else {
        $("#ddlFK_ID").attr("disabled", ""); //部门启用
        var cy = $("#ddlCompany").val();
        $.ajax({
            type: "POST",
            url: "addJob.aspx/ddlData",
            data: "{'id':'" + cy + "'}",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function (data) {
                //返回的数据用data.d获取内容  
                $("option", "#ddlFK_ID").empty(); //清空下拉框值
                $('<option value=""></option>').appendTo("#ddlFK_ID"); //赋空值

                var dataobj = eval("(" + data.d + ")"); //转换为json对象
                $.each(dataobj.root, function (i, item) { //循环
                    $('<option value="' + item.name + '">' + item.value + '</option>').appendTo("#ddlFK_ID");
                })

                var h = $("input[id$='hfid']").val();
                $("#ddlFK_ID").attr("value", h);
                
            },
            error: function (err) {
                alert(err);
            }
        });


    }

    //公司下拉框选中事件
    $("#ddlCompany").change(function () {
        var c = $("#ddlCompany").find("option:selected").text();

        if (c == "") {
            $("#ddlFK_ID").attr("disabled", "disabled"); //部门禁用
            $("#ddlFK_ID").empty(); //清空下拉框值
            $("input[id$='hfid']").val(""); //清空
        } else {
            $("#ddlFK_ID").attr("disabled", ""); //部门启用
            var cy = $("#ddlCompany").val();

            $.ajax({
                type: "POST",
                url: "addJob.aspx/ddlData",
                data: "{'id':'" + cy + "'}",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    //返回的数据用data.d获取内容  
                    $("option", "#ddlFK_ID").remove(); //清空下拉框值
                    $('<option value=""></option>').appendTo("#ddlFK_ID"); //赋空值

                    var dataobj = eval("(" + data.d + ")"); //转换为json对象
                    $.each(dataobj.root, function (i, item) { //循环
                        $('<option value="' + item.name + '">' + item.value + '</option>').appendTo("#ddlFK_ID");
                    })

                    var h = $("input[id$='hfid']").val();
                    $("#ddlFK_ID").attr("value", h);
                },
                error: function (err) {
                    alert(err);
                }
            });
        }
    })


    $("#ddlFK_ID").change(function () {
        var id = $("#ddlFK_ID").val();  //获取部门ID
        if (id != "") {
            $("input[id$='hfid']").val(id); //赋值
        } else {
            $("input[id$='hfid']").val(""); //清空
        }

    })

})